//
//  DateSelectPicker.m
//  OA-JZD
//
//  Created by apple on 2018/2/1.
//  Copyright © 2018年 JC. All rights reserved.
//

#import "DateSelectPicker.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define BACKGROUND_BLACK_COLOR [UIColor colorWithRed:0.412 green:0.412 blue:0.412 alpha:0.7]
static const NSInteger SelectPickerViewDefaultHeight = 248;
static const NSInteger SelectToolBarHeight = 44;
//@implementation SelectPickerComponentObject
//- (instancetype)init{
//    return [self initWithText:@"" subArray:[NSMutableArray array]];
//}
//
//- (instancetype)initWithText:(NSString *)text{
//    return [self initWithText:text subArray:[NSMutableArray array]];
//}
//
//
//- (instancetype)initWithText:(NSString *)text subArray:(NSMutableArray *)array{
//    if (self = [super init]) {
//        _text = text;
//        _subArray = array;
//    }
//    return self;
//}
//
//@end

@implementation DateSelectPickerBuilder

- (instancetype)init{
    if (self = [super init]) {
        _showMask = YES;
        _defaultIndex = 0;
        _pickerHeight = SelectPickerViewDefaultHeight;
        _datePickerMode = UIDatePickerModeDateAndTime;
    }
    return self;
}
@end




@interface DateSelectPicker()
@property (nonatomic, strong) DateSelectPickerBuilder *builder;
@property (nonatomic, copy) DateSelectPickerConfirmBlock confirmBlock;
@property (nonatomic, copy) DateSelectPickerCancelBlock cancelBlock;
@property (nonatomic, strong) UIDatePicker *datepickerView;
@property (nonatomic, strong) UIButton *confirmButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIView *containerView;

@end

@implementation DateSelectPicker

- (void)config{
    [self setupViews];
}
+ (void)showDatePickerInView:(UIView *)view
                withBuilder:(nullable DateSelectPickerBuilder *)builder
                    confirm:(DateSelectPickerConfirmBlock)confirmBlock
                     cancel:(DateSelectPickerCancelBlock)cancelBlcok{
    DateSelectPicker *pickerView = [[DateSelectPicker alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    pickerView.confirmBlock = confirmBlock;
    pickerView.cancelBlock = cancelBlcok;
    pickerView.builder = builder ?:[DateSelectPickerBuilder new];
    [pickerView config];
    [view addSubview:pickerView];
}


- (void)setupViews{
    self.backgroundColor = self.builder.isShowMask ? BACKGROUND_BLACK_COLOR : UIColor.clearColor;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissView)];
    [self addGestureRecognizer:tap];
    [self addSubview:self.containerView];
    
    [self.containerView addSubview:self.datepickerView];
    [self.containerView addSubview:self.titleLabel];
    [self.containerView addSubview:self.confirmButton];
    [self.containerView addSubview:self.cancelButton]; 
}


#pragma mark - event response

- (void)confirm:(UIButton *)button{
    if (self.confirmBlock){
        self.confirmBlock(self.datepickerView.date);
    }
    [self removeFromSuperview];
}

- (void)dissView{
    if (self.cancelBlock) {
        self.cancelBlock();
    }
    [self removeFromSuperview];
}

- (void)cancel:(UIButton *)button{
    [self dissView];
}

#pragma mark - getter

- (UIView *)containerView{
    if (!_containerView) {
        CGFloat pickerViewHeight = self.builder.pickerHeight > SelectToolBarHeight ? self.builder.pickerHeight : SelectPickerViewDefaultHeight;
        _containerView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - pickerViewHeight, SCREEN_WIDTH, pickerViewHeight)];
        _containerView.backgroundColor = self.builder.pickerColor ?: UIColor.whiteColor;
        UIView *topview = [[UIView alloc]initWithFrame:CGRectMake(0,  0, SCREEN_WIDTH, 40)];
        topview.backgroundColor= RGBCOLOR(247, 247, 247);
        [_containerView addSubview:topview];
    }
    return _containerView;
}



- (UIButton *)confirmButton{
    if (!_confirmButton) {
        _confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -50, 5, 40, 30)];
        _confirmButton.backgroundColor = UIColor.clearColor;
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
        NSString *title = self.builder.confirmText.length ? self.builder.confirmText : @"确定";
        [_confirmButton setTitle:title forState:UIControlStateNormal];
        UIColor *color = self.builder.confirmTextColor ?: UIColor.blueColor;//;
        [_confirmButton setTitleColor:color forState:UIControlStateNormal];
        [_confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, SJScreenW, 30)];
        _titleLabel.backgroundColor = UIColor.clearColor;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:18.0];
        _titleLabel.text = self.builder.titleText.length ? self.builder.titleText : @"";
    }
    return _titleLabel;
}


- (UIButton *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 30)];
        _cancelButton.backgroundColor = UIColor.clearColor;
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
        NSString *title = self.builder.cancelText.length ? self.builder.cancelText : @"取消";
        [_cancelButton setTitle:title forState:UIControlStateNormal];
        UIColor *color = self.builder.cancelTextColor ?: UIColor.blueColor;//;
        [_cancelButton setTitleColor:color forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}


- (UIDatePicker *)datepickerView{
    if (!_datepickerView) {
        CGFloat pickerViewHeight = _containerView.frame.size.height;
        _datepickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,SelectToolBarHeight, SCREEN_WIDTH, pickerViewHeight - SelectToolBarHeight)];
        _datepickerView.backgroundColor =  UIColor.clearColor;
        NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];//设置为中文zh_CN
        _datepickerView.locale = locale;
//        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
//        self.pickView.locale = locale;
//    _datepickerView.date = [NSDate alloc]inite
        _datepickerView.datePickerMode = self.builder.datePickerMode ? self.builder.datePickerMode:UIDatePickerModeTime;
        [_datepickerView addTarget:self action:@selector(datePickChanged:) forControlEvents:UIControlEventValueChanged];
        
        
        //    NSDate *date = [NSDate date];
//        if(self.builder.lastSelectDate){
//            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
//            NSDate *date = [dateFormatter dateFromString:self.builder.lastSelectDate];//上次设置的日期
//
//            if (!self.builder.lastSelectDate) {
//                date = [NSDate date];
//            }
//            // 2.3 将转换后的日期设置给日期选择控件
//            [datePicker setDate:date];
//        }
        
     
        if(self.builder.minDate){
            //设置日期最大及最小值
            _datepickerView.minimumDate = self.builder.minDate;
        }
        if(self.builder.maxDate){
            //设置日期最大及最小值
            _datepickerView.maximumDate = self.builder.maxDate;
        }
        if (self.builder.lastSelectDate) {
            [_datepickerView setDate:self.builder.lastSelectDate];
        }
//        NSDate *  timeDate=[NSDate date];
//        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//        fmt.dateFormat = @"yyyy-MM-dd";
//
//        NSDate *minDate = [fmt dateFromString:@"1930-1-1"];
//        //设置日期最大及最小值
//        _datepickerView.maximumDate = timeDate;
//        _datepickerView.minimumDate = minDate;
        
        //设置字体颜色
//        [_datepickerView setValue:kColor3C3C3C forKeyPath:@"textColor"];

    }
    return _datepickerView;
}


-(void)datePickChanged:(id)sender{
    UIDatePicker *control =(UIDatePicker *)sender;
    NSDate *mydate = control.date;
    NSLog(@"mydate--%@",mydate);
//    if (self.confirmBlock){
//        self.confirmBlock(self.datepickerView.date);
//    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
